package com.ljh.controller;


import com.ljh.common.JsonData;
import com.ljh.pojo.Client;
import com.ljh.pojo.Reservation;
import com.ljh.service.CLIENTervice;
import com.ljh.service.DINDINGervice;
import com.ljh.service.RESERVATIONervice;
import com.ljh.vo.DindingVo;
import com.ljh.vo.ReservationVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘剑鸿
 * @since 2021-10-25
 */
@Controller
@RequestMapping("reservation")
public class ReservationController {

    @Autowired
    private CLIENTervice clienTervice;
    @Autowired
    private RESERVATIONervice reservatioNervice;
    @Autowired
    private DINDINGervice dindinGervice;
    /*新增预约*/
    @RequestMapping("appReservation")
    public String add(Reservation reservation,Client client) throws ParseException {
        clienTervice.addClient(client);
        int clientId = client.getClientid();
        reservation.setClientid(clientId);

        SimpleDateFormat sim1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDate=new SimpleDateFormat("hh:mm");
        Date date = reservation.getReservatime();
        Date date1 = reservation.getHhmmtime();
        String time=simpleDateFormat.format(date);
        String time1 = simpleDate.format(date1);
        String time3 = time+" "+time1+":00";
        Date datetime = sim1.parse(time3);

        reservation.setToReservatime(datetime);
        reservatioNervice.addRes(reservation);
//        System.out.println(time1);
//        System.out.println(time3);
//        System.out.println(datetime);
        return "redirect:/user/index";
    }

    /*预约查询*/
    @RequestMapping("loadRes")
    @ResponseBody
    public JsonData loadRes(ReservationVo reservationVo){
        return reservatioNervice.selectres(reservationVo);
    }
    @RequestMapping("Stoptable")
    @ResponseBody
    public JsonData Stoptable(DindingVo dindingVo){
        return reservatioNervice.selectStop(dindingVo);
    }
    /*从预约开台*/
    @RequestMapping("UpdateRes")
    @ResponseBody
    public JsonData UdataRes(@RequestParam(value = "id",required = true) Integer id){
        try {
            reservatioNervice.ToOutTable(id);
            return JsonData.success("开台成功！");

        }
        catch (Exception e){
            return JsonData.fail(e.getMessage());
        }
    }
    @RequestMapping("Opentable")
    @ResponseBody
    public JsonData Opentable(@RequestParam(value = "id",required = true) Integer id){
        try {//启用的
            dindinGervice.qiyong(id);
            return JsonData.success("启用成功！");
        }
        catch (Exception e){
            return JsonData.fail(e.getMessage());
        }
    }

    @RequestMapping("Stopdintable")
    @ResponseBody
    public JsonData Stopdintable(@RequestParam(value = "id",required = true) Integer id){
        try {//停用的
            dindinGervice.tingyo(id);
            return JsonData.success("停用成功！");
        }
        catch (Exception e){
            return JsonData.fail(e.getMessage());
        }
    }
    /*删除预约*/
    @RequestMapping("DelectRes")
    @ResponseBody
    public JsonData DelectRes(@RequestParam(value = "id",required = true) Integer id){
        try{
            reservatioNervice.ToDelectRes(id);
            return JsonData.success("删除成功！");
        }
        catch (Exception e){
            return JsonData.fail(e.getMessage());
        }
    }
}

